<?php
namespace app\index\controller;

use app\api\model\IndentQxModel;
use app\api\model\YuyueModel;
use app\services\MessageServices;
use think\Controller;
use think\Db;
use app\api\model\IndentModel;
use app\api\model\QrcodeModel as Qrcode;
use app\api\model\BankModel as Bank;

class Index extends Controller{

    protected $order;
    protected $qrcode;
    protected $bank;
    public function __construct()
    {
        parent::__construct();
        $this->order = new IndentModel();
        $this->qrcode = new Qrcode();
        $this->bank = new Bank();
    }

    //订单显示
    public function order(){
        if(!empty($_GET['ordersn'])){
            $ordersn = $_GET['ordersn'];
            $res = $this->order->where("ordersnmd5",$ordersn)->find();
            if($res){
                $payinfo = '';
                if($res['sid'] != 1 && $res['uid'] != ''){
                    if($res['zid'] != 3){
                        $payinfo = $this->qrcode->where("q_state",1)->where("zid",$res['zid'])->where("uid",$res['uid'])->find();
                    }else{
                        $payinfo = $this->bank->where("b_state",1)->where("uid",$res['uid'])->find();
                    }
                }
                $this->assign("payinfo",$payinfo);
                $this->assign("orderInfo",$res);
                return $this->fetch();
            }else{
                return json_encode(array("code"=>"-90002","msg"=>"没有该订单"));
            }
        }else{
            return json_encode(array("code"=>"-90001","msg"=>"参数错误"));
        }
    }

    //订单确认
    public function order_check(){
        if($_POST['type']==1){//确认
            $indent=IndentModel::where('tid',$_POST['tid'])
                ->update([
                    'sid'=>4,
                    'z_datime'=>date('Y-m-d H:i:s')
                ]);
            if($indent){
                $this->success("确认成功",'','1');
            }else{
                $this->error("确认失败",'','0');
            }
        }else{//取消
            $shiwu=new IndentModel();
            $shiwu->startTrans();
            $indent=IndentModel::where('tid',$_POST['tid'])->find();//订单信息
            $indent_qx=IndentQxModel::insert([//添加取消订单
                'tid'=>$indent['tid'],
                't_number'=>$indent['t_number'],
                't_money'=>$indent['t_money'],
                'z_name'=>$indent['z_name'],
                'uid'=>$indent['uid'],
                'zid'=>$indent['zid'],
                'z_createtime'=>$indent['z_createtime'],
                'z_cancletime'=>date('Y-m-d H:i:s')
            ]);
            $yuyue=YuyueModel::where('tid',$indent['tid'])//修改预定信息
                ->update([
                    'tid'=>'',
                    'y_state'=>0
                ]);
            $re1=IndentModel::where('tid',$_POST['tid'])//修改订单信息
                ->update([
                    'uid'=>'',
                    'sid'=>1
                ]);
            $re2=$this->delivery($indent['tid']);
            if($indent_qx&&$yuyue&&$re1&&$re2){
                $shiwu->commit();
                $this->success("取消成功",'','1');
            }else{
                $shiwu->rollback();
                $this->error("取消失败",'','0');
            }
        }
    }
    //寻找符合条件的抢单用户
    public function delivery($tid)
    {
        $indent=IndentModel::where('tid',$tid)->find();//订单信息
        $yuyue=YuyueModel::alias('a')
            ->join('jc_user b','a.uid=b.uid','left')
            ->where('a.zid',$indent['zid'])
            ->where('b.u_money','>',$indent['t_money'])
            ->where('yid','>',$indent['sort'])
            ->order('yid asc')
            ->field('a.*,b.u_money')
            ->find();
        if(!empty($yuyue)){//下面还有排队等单的人 就执行
            $re=YuyueModel::where('yid',$yuyue['yid'])//订单传递给下一个人
            ->update([
                'tid'=>$tid,
                'y_state'=>2
            ]);
            $re2=IndentModel::where('tid',$tid)->update(['sort'=>$yuyue['yid']]);
            if($re&&$re2){//向下一个人推送消息
                //TODO ADFSA
                $re=MessageServices::pushmessage($yuyue['uid'],$tid,$yuyue['yid']);
                if($re=="ok"){
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            return true;
        }
    }
}
?>